import { Color } from 'cc';
import { _decorator, Component, Sprite, SpriteFrame } from 'cc';
const { ccclass, property, requireComponent } = _decorator;

/**
 * 248139929
 * Wed Apr 26 2023 15:16:34 GMT+0800 (中国标准时间)
 * 多个精灵状态的显示
 */

@ccclass('SpriteFramesComponent')
@requireComponent(Sprite)
export class SpriteFramesComponent extends Component {
    @property(Sprite)
    sprite:Sprite;

    @property([SpriteFrame])
    spriteFrames:SpriteFrame[] = [];

    protected onLoad(): void {
        if(!this.sprite){
            this.sprite = this.getComponent(Sprite);
        }
    }

    /**按索引选择Sprite对象,如果超出索引则选最后一个 */
    selectSpritFrame(idx:number){
        this.sprite.spriteFrame = this.spriteFrames[Math.min(this.spriteFrames.length - 1, idx)];
        // if(sf == null){
        //     this.sprite.enabled = false;
        // }else{
        //     this.sprite.enabled = true;
        //      = sf;
        // }
    }

    /**按索引选择Sprite对象,如果超出索引或者未空时则隐藏自己 */
    selectSpritFrameToHide(idx:number){
        if(idx < this.spriteFrames.length){
            let sp = this.spriteFrames[idx]
            if(sp){
                this.node.active = true
                this.sprite.spriteFrame = sp
            }else{
                this.node.active = false
            }
        }else{
            this.node.active = false
        }
    }

    set grayscale(v:boolean){
        this.sprite.grayscale = v;
    }

    set color(c:Color){
        this.sprite.color = c;
    }

    get maxIdx(){
        return this.spriteFrames.length;
    }
}


